package com.demo7;

import com.github.unidbg.AndroidEmulator;
import com.github.unidbg.Emulator;
import com.github.unidbg.file.FileIO;
import com.github.unidbg.file.FileResult;
import com.github.unidbg.file.IOResolver;
import com.github.unidbg.linux.file.ByteArrayFileIO;
import com.github.unidbg.linux.file.SimpleFileIO;
import com.github.unidbg.pointer.UnidbgPointer;
import com.github.unidbg.unix.FileListener;
import com.sun.jna.Pointer;

import java.io.File;
import java.nio.charset.StandardCharsets;

public class TDIO implements IOResolver {
    @Override
    public FileResult resolve(Emulator emulator, String pathname, int oflags) {
        System.out.println("file open:"+pathname);
        if(pathname.contains("maps")){
            return FileResult.success(new SimpleFileIO(oflags, new File("unidbg-android/src/test/resources/demo7/proc/testmaps"), pathname));
        }
        if(pathname.equals("/proc/stat")) {
            String stat = "cpu  1002987 235888 1507452 8562633 65509 168534 133761 0 0 0\n" +
                    "cpu0 219034 53263 507842 5848732 63587 66671 59783 0 0 0\n" +
                    "cpu1 202983 48517 343376 369233 437 35671 24115 0 0 0\n" +
                    "cpu2 236838 38306 280565 370174 743 29818 24464 0 0 0\n" +
                    "cpu3 110295 15225 217648 372748 212 30639 21688 0 0 0\n" +
                    "cpu4 53625 18817 37047 398304 245 1391 872 0 0 0\n" +
                    "cpu5 48080 19416 32243 401410 67 1231 755 0 0 0\n" +
                    "cpu5 48080 19416 32243 401410 67 1231 755 0 0 0\n" +
                    "cpu6 70217 22405 32952 400331 114 1638 1164 0 0 0\n" +
                    "cpu7 61915 19939 55779 401696 101 1471 918 0 0 0\n" +
                    "intr 114927529 0 0 0 0 12143762 0 2680991 113103 0 0 0 0 1641960 0 77 0 136670 13379 30768 2726139 0 2 0 0 0 1563 566 11 11 0 0 0 0 0 0 0 0 0 0 0 0 0 734 0 84 0 0 0 0 0 0 376162 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 21 0 0 0 0 0 0 0 0 7 0 0 1 0 0 0 0 0 511 0 0 0 0 0 0 0 23 7492291 120263 17304 100552 651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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1484 361 0 6 187 536 0 195 0 0 0 0 0 0 0 0 0 0 0 0 0 394 9 13 0 12 139 1587 16 0 0 0 0 0 0 0 0 886 0 816 1355 7036 0 21 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 827177 0 10 0 0 223 0 0 7 10 0 0 0 0 10 0 0 344383 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
                    "ctxt 209018694\n" +
                    "btime 1638246557\n" +
                    "processes 179999\n" +
                    "procs_running 1\n" +
                    "procs_blocked 0\n" +
                    "softirq 36096590 7868433 8283497 353788 1021834 2497468 0 3752124 6383970 0 5935476";

            return FileResult.success(new ByteArrayFileIO(oflags, pathname, stat.getBytes()));
        }
        // 查看当前进程名
        if (pathname.contains("cmdline")) {
            return FileResult.success(new ByteArrayFileIO(oflags, pathname, "com.hundsun.qy.hospitalcloud.bj.xhhosp.hsyy\0".getBytes()));
        }
        if(pathname.equals("id")){
            return FileResult.success(new ByteArrayFileIO(oflags, pathname, "uid=2000(shell) gid=2000(shell) groups=2000(shell),1004(input),1007(log),1011(adb),1015(sdcard_rw),1028(sdcard_r),3001(net_bt_admin),3002(net_bt),3003(inet),3006(net_bw_stats),3009(readproc),3011(uhid) context=u:r:shell:s0".getBytes(StandardCharsets.UTF_8)));
        }
        if("/proc/version".equals(pathname)){
            return FileResult.success(new SimpleFileIO(oflags, new File("unidbg-android/src/test/resources/demo7/proc/version"), pathname));
        }
        if("/proc/cpuinfo".equals(pathname)){
            return FileResult.success(new ByteArrayFileIO(oflags, pathname, "1766400".getBytes()));
        }

        // 查看cpu最大频率
        // https://bbs.pediy.com/thread-229579-1.htm
        if("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq".equals(pathname)){
            return FileResult.success(new ByteArrayFileIO(oflags, pathname, "1766400".getBytes()));
        }
        // 读两个status，不懂想干啥，可能是tracerpid
        if (("/proc/" + emulator.getPid() + "/status").equals(pathname) || "/proc/self/status".equals(pathname)) {
//            System.out.println("Call status");
            String status = "Name:   dhgate.buyermob\n" +
                    "Umask:  0077\n" +
                    "State:  S (sleeping)\n" +
                    "Tgid:   "+emulator.getPid()+"\n" +
                    "Ngid:   0\n" +
                    "Pid:    "+emulator.getPid()+"\n" +
                    "PPid:   1682\n" +
                    "TracerPid:      0\n" +
                    "Uid:    10256   10256   10256   10256\n" +
                    "Gid:    10256   10256   10256   10256\n" +
                    "FDSize: 512\n" +
                    "Groups: 3001 3003 9997 20256 50256 99909997\n" +
                    "VmPeak:  2557156 kB\n" +
                    "VmSize:  2386280 kB\n" +
                    "VmLck:         0 kB\n" +
                    "VmPin:         0 kB\n" +
                    "VmHWM:    252796 kB\n" +
                    "VmRSS:    226608 kB\n" +
                    "RssAnon:           92552 kB\n" +
                    "RssFile:          130844 kB\n" +
                    "RssShmem:           3212 kB\n" +
                    "VmData:  1401328 kB\n" +
                    "VmStk:      8192 kB\n" +
                    "VmExe:        20 kB\n" +
                    "VmLib:    296792 kB\n" +
                    "VmPTE:      1900 kB\n" +
                    "VmPMD:        16 kB\n" +
                    "VmSwap:    14444 kB\n" +
                    "Threads:        81\n" +
                    "SigQ:   0/21555\n" +
                    "SigPnd: 0000000000000000\n" +
                    "ShdPnd: 0000000000000000\n" +
                    "SigBlk: 0000000080001200\n" +
                    "SigIgn: 0000000000000001\n" +
                    "SigCgt: 0000000e400086fc\n" +
                    "CapInh: 0000000000000000\n" +
                    "CapPrm: 0000000000000000\n" +
                    "CapEff: 0000000000000000\n" +
                    "CapBnd: 0000000000000000\n" +
                    "CapAmb: 0000000000000000\n" +
                    "Seccomp:        2\n" +
                    "Speculation_Store_Bypass:       unknown\n" +
                    "Cpus_allowed:   07\n" +
                    "Cpus_allowed_list:      0-2\n" +
                    "Mems_allowed:   1\n" +
                    "Mems_allowed_list:      0\n" +
                    "voluntary_ctxt_switches:        33007\n" +
                    "nonvoluntary_ctxt_switches:     5564";

            return FileResult.success(new ByteArrayFileIO(oflags, pathname, status.getBytes()));
        }
        if(pathname.equals("/data/app/com.hundsun.qy.hospitalcloud.bj.xhhosp.hsyy-4j4PfNvJ27HiE6sPnXQ3gg==/base.apk")){
            return FileResult.success(new SimpleFileIO(oflags, new File("unidbg-android/src/test/resources/demo7/apkFiles/crack_v2.20.apk"), pathname));
        }
        return null;
    }
}
